﻿using System;
using System.Threading;
using System.Threading.Tasks;
using UpperComputer.Application;
using UpperComputer.WPF.Entry.Common;
using UpperComputer.WPF.Entry.Common.Extensions;
using YFurion.Application;

namespace UpperComputer.WPF.Entry.ViewModels
{
    public class AuditLogViewModel : ManagementViewModel
    {
        private readonly IAuditLogService _auditLogService;

        public AuditLogResultRequestDto Query { get; } = new AuditLogResultRequestDto();

        private PagedResultDto<AuditLogDto> _auditLogPaged = new PagedResultDto<AuditLogDto>();
        public PagedResultDto<AuditLogDto> AuditLogPaged
        {
            get => _auditLogPaged;
            set => SetProperty(ref _auditLogPaged, value);
        }

        public AuditLogViewModel(IAuditLogService auditLogService)
        {
            _auditLogService = auditLogService;
        }

        protected override async Task LoadAsync(object parameter, CancellationToken cancellationToken)
        {
            if (Query.StartTime != null && Query.EndTime != null && DateTime.Compare(Query.StartTime.Value, Query.EndTime.Value) >= 0)
            {
                Snackbar.ShowWarningMessage("结束时间不能小于开始时间");
                return;
            }
            SetCurrent(parameter);
            SetSkipCount(Query);
            AuditLogPaged = await _auditLogService.GetAll(Query, cancellationToken);
        }
    }
}
